//
//  SaleDetailsNaviBar.swift
//  TestDemo
//
//  Created by ayin on 2019/5/30.
//  Copyright © 2019 aYin. All rights reserved.
//

import UIKit

protocol SaleDetailsNaviBarDelegate : NSObjectProtocol {
    func backBtnActionClicked()
    func guideBtnActionClicked()
    func moreBtnActionClicked()
}

class SaleDetailsNaviBar: UIView {
    
    lazy var coverView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.init(hexString: "707AFC")
        v.alpha = 0.0
        return v
    }()
    
    lazy var backBtn : UIButton = {
        let backBtn = UIButton(type: .custom)
        backBtn.setImage(UIImage(named: "back_icon_gary_01"), for: .normal)
        backBtn.setImage(UIImage(named: "back_icon_gary_01"), for: .highlighted)
        backBtn.addTarget(self, action: #selector(backBtnAction(sender:)), for: .touchUpInside)
        return backBtn
    }()
    
    lazy var titleLabel : UILabel = {
        let titleLabel = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 17.up), text: "发售详情", textColor: UIColor.getCommonColor(with: .ColorWhiteType), numberLine: 1, alignment: .center)
        return titleLabel
    }()
    
    lazy var guideBtn: UIButton = {
        let button = UIButton(frame: .zero)
//        button.directType = .imageTop
//        button.space = 2
        button.setTitle("抽签指南", for: .normal)
        button.setTitleColor(UIColor.getCommonColor(with: .ColorWhiteType), for: .normal)
        button.titleLabel?.font = UIFont.pingfangRegular(size: 10.up)
        button.setImage(UIImage(named: "sale_detail_guide"), for: .normal)
        button.addTarget(self, action: #selector(guideBtnClickAction(sender:)), for: .touchUpInside)
        return button
    }()
    
    lazy var moreBtn : UIButton = {
        let moreBtn = UIButton(type: .custom)
        moreBtn.setImage(UIImage(named: "more_icon_share"), for: .normal)
        moreBtn.setImage(UIImage(named: "more_icon_share"), for: .highlighted)
        moreBtn.addTarget(self, action: #selector(moreBtnAction(sender:)), for: .touchUpInside)
        return moreBtn
    }()
    
    weak var delegate: SaleDetailsNaviBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(coverView)
        coverView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        addSubview(backBtn)
        backBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.bottom.equalToSuperview().offset(-2)
            make.size.equalTo(CGSize(width: 55, height: 40))
        }
        
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(backBtn.snp.centerY)
            make.centerX.equalToSuperview()
        }
        
        addSubview(moreBtn)
        moreBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(backBtn.snp.centerY)
            make.right.equalToSuperview().offset(-15)
            make.size.equalTo(CGSize(width: 22.up, height: 22.up))
        }
        
        addSubview(guideBtn)
        guideBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(backBtn.snp.centerY)
            make.left.equalTo(titleLabel.snp.right).offset(20)
            make.right.equalTo(moreBtn.snp.left).offset(-20).priority(1000)
            make.size.equalTo(CGSize(width: 40.up, height: 40.up))
        }
    }
    
    public func update(title: String) {
        self.titleLabel.text = title
    }
    
    public func updateAlpha(alpha: CGFloat) {
        self.coverView.alpha = alpha
    }
    
}

extension SaleDetailsNaviBar {
    
    @objc func backBtnAction(sender:UIButton) {
        delegate?.backBtnActionClicked()
    }
    
    @objc func guideBtnClickAction(sender:UIButton) {
        delegate?.guideBtnActionClicked()
    }
    
    @objc func moreBtnAction(sender:UIButton) {
        delegate?.moreBtnActionClicked()
    }
}
